package com.kara.woodAgent.web.user.service.impl;

import cn.dev33.satoken.stp.SaLoginModel;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kara.woodAgent.common.exception.BizException;
import com.kara.woodAgent.common.util.PasswordUtil;
import com.kara.woodAgent.web.user.domain.WgUser;
import com.kara.woodAgent.web.user.mapper.WgUserMapper;
import com.kara.woodAgent.web.user.model.request.LoginRequest;
import com.kara.woodAgent.web.user.model.request.RegisterRequest;
import com.kara.woodAgent.web.user.model.vo.LoginVo;
import com.kara.woodAgent.web.user.service.WgUserService;
import jakarta.annotation.PostConstruct;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Duration;

import static com.kara.woodAgent.common.constant.BaseConst.*;

/**
 * @author tzy
 * @description 针对表【wg_user】的数据库操作Service实现
 * @createDate 2025-06-07 01:29:33
 */
@Service
public class WgUserServiceImpl extends ServiceImpl<WgUserMapper, WgUser>
        implements WgUserService {




    @Autowired
    private RedissonClient redissonClient;


    private RBloomFilter<Object> bloomFilter;


    @PostConstruct
    public void init() {
        bloomFilter = redissonClient.getBloomFilter(NICK_NAME_BLOOM_FILTER);
        bloomFilter.tryInit(10000, 0.01);
    }

    @Override
    public LoginVo login(LoginRequest loginRequest) {

        LambdaQueryWrapper<WgUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WgUser::getUsername, loginRequest.getUsername())
                .eq(WgUser::getEmail, loginRequest.getEmail());

        WgUser user = this.getOne(queryWrapper);
        if (user == null) {
            throw new BizException("用户不存在");
        }

        String password = user.getPassword();
        if (!PasswordUtil.checkPassword(loginRequest.getPassword(), password)) {
            throw new BizException("密码错误");
        }

        LoginVo loginVo = new LoginVo(user);
        StpUtil.login(user.getId(), new SaLoginModel().setIsLastingCookie(true).setIsWriteHeader(true).setTimeout(60 * 60 * 24));
        String tokenValue = StpUtil.getTokenValue();
        redissonClient.getBucket(USER_LOGIN + ":" + tokenValue).set(loginVo, Duration.ofSeconds(60 * 60 * 24));

        return loginVo;
    }

    @Override
    public void register(RegisterRequest registerRequest) {

        String username = registerRequest.getUsername();

        LambdaQueryWrapper<WgUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WgUser::getUsername, username);

        if (this.exists(queryWrapper)) {
            throw new BizException("用户名已存在");
        }

        String email = registerRequest.getEmail();
        queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WgUser::getEmail, email);
        if (this.exists(queryWrapper)) {
            throw new BizException("邮箱已存在");
        }


        //生成nickname
        String nickNAme = getRandomName();
        WgUser wgUser = new WgUser();
        wgUser.setUsername(username);
        wgUser.setPassword(PasswordUtil.encryptPassword(registerRequest.getPassword()));
        wgUser.setEmail(email);
        wgUser.setNickname(nickNAme);
        wgUser.setAvatar(registerRequest.getAvatar());

        this.save(wgUser);
    }

    private String getRandomName() {

        String s;

        do {
            s = RandomUtil.randomString(6);
        } while (bloomFilter.contains(s));

        return NICK_NAME_PREFIX + "_" + s;

    }
}




